#include <buola/gui.h>

#include <buola/gui/x11/cx11dndfilter.h>
#include <buola/gui/dnd/cdrag.h>
#include <buola/gui/x11/cx11drag.h>
#include <buola/gui/dnd/cdrop.h>
#include <buola/gui/x11/cx11drop.h>

#include <buola/gui/x11/cx11toolkit.h>
#include <buola/gui/x11/cx11display.h>
#include <buola/gui/x11/cx11window.h>

namespace buola { namespace gui {

using namespace x11;

CX11DndFilter::CX11DndFilter()
{
    get_x11_toolkit()->GetDispatcher()->AddClientMessageFilter(this);

    mCurrentDrag=nullptr;
    mCurrentDrop=nullptr;
}

void CX11DndFilter::SetCurrentDrag(CDrag *pDrag)
{
    mCurrentDrag=pDrag;
}

bool CX11DndFilter::OnX11ClientMessage(XClientMessageEvent *pE)
{
///\todo
#if 0
    if(pE->message_type==get_default_x11_display()->Atoms().mDndStatus)
    {
        if(mCurrentDrag)
            ((CX11Drag*)mCurrentDrag->Impl())->ProcessDndStatus(pE);
        return false;
    }
    else if(pE->message_type==get_default_x11_display()->Atoms().mDndEnter)
    {
        CWindow *lW=CX11Window::GetWindow(pE->window);
        if(mCurrentDrop) delete mCurrentDrop;
        mCurrentDrop=new CDrop();
        ((CX11Drop*)mCurrentDrop->Impl())->mMessageWindow=lW;
        ((CX11Drop*)mCurrentDrop->Impl())->ProcessDndEnter(pE);

        return false;
    }
    else if(pE->message_type==get_default_x11_display()->Atoms().mDndPosition)
    {
        if(!mCurrentDrop)
        {
            msg_warn() << "No mCurrentDrop when receiving DndPosition\n";
            return false;
        }

        CWindow *lW=CX11Window::GetWindow(pE->window);
        if(lW!=((CX11Drop*)mCurrentDrop->Impl())->mMessageWindow)
        {
            msg_warn() << "mMessageWindow changed unexpectedly\n";
            ((CX11Drop*)mCurrentDrop->Impl())->mMessageWindow=lW;
        }

        ((CX11Drop*)mCurrentDrop->Impl())->ProcessDndPosition(pE);

        return false;
    }
    else if(pE->message_type==get_default_x11_display()->Atoms().mDndLeave)
    {
        if(!mCurrentDrop)
        {
            msg_warn() << "No mCurrentDrop when receiving DndLeave\n";
            return false;
        }

        CWindow *lW=CX11Window::GetWindow(pE->window);
        if(lW!=((CX11Drop*)mCurrentDrop->Impl())->mMessageWindow)
        {
            msg_warn() << "mMessageWindow changed unexpectedly in DndLeave\n";
            return false;
        }

        ((CX11Drop*)mCurrentDrop->Impl())->ProcessDndLeave(pE);

        return false;
    }
    else if(pE->message_type==get_default_x11_display()->Atoms().mDndDrop)
    {
        if(!mCurrentDrop)
        {
            msg_warn() << "No mCurrentDrop when receiving DndDrop\n";
            return false;
        }

        CWindow *lW=CX11Window::GetWindow(pE->window);
        if(lW!=((CX11Drop*)mCurrentDrop->Impl())->mMessageWindow)
        {
            msg_warn() << "mMessageWindow changed unexpectedly in DndDrop\n";
            return false;
        }

        ((CX11Drop*)mCurrentDrop->Impl())->ProcessDndDrop(pE);

        return false;
    }
#endif
    return true;
}

/*namespace gui*/ } /*namespace buola*/ }
